//
//  CYAlert.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "HYAlertView.h"
#define HYAlertMessageFont [UIFont systemFontOfSize:15 weight:UIFontWeightLight]
@interface HYAlertController ()
@property (nonatomic, strong) UILabel *customLabel;
@end
@implementation HYAlertController
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    for (UIView *view in [self.view subviews])
    {
        view.layer.cornerRadius = 5.0;
        view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        view.layer.borderWidth = 0.3;
        view.layer.masksToBounds = YES;
        [view removeFromSuperview];
    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (UILabel *)customLabel
{
    if (!_customLabel)
    {
        _customLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _customLabel.bounds = CGRectMake(0.0, 0.0, 150, 40);
        _customLabel.center = CGPointZero;
        _customLabel.backgroundColor = [UIColor whiteColor];
        _customLabel.layer.cornerRadius = 5.0;
        _customLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _customLabel.layer.borderWidth = 0.3;
        _customLabel.layer.masksToBounds = YES;
        _customLabel.textAlignment = NSTextAlignmentCenter;
        [self setupCustomLabelText];
        [self.view addSubview:_customLabel];
    }
    return _customLabel;
}

- (void)setupCustomLabelText
{
    
    CGSize size = [self.message sizeWithAttributes:@{NSFontAttributeName:HYAlertMessageFont}];
    _customLabel.bounds = CGRectMake(0.0, 0.0, ceil(size.width) + 40, 40);
    NSMutableAttributedString *alertVCMessage = [[NSMutableAttributedString alloc] initWithString:self.message];

    [alertVCMessage addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.message.length)];
    
    [alertVCMessage addAttribute:NSFontAttributeName value:HYAlertMessageFont range:NSMakeRange(0, self.message.length)];
    
    _customLabel.attributedText = alertVCMessage;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.customLabel.hidden = NO;
}
@end


@interface HYAlertView ()
@property (nonatomic, strong) HYLabel *label;
@end

@implementation HYAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.layer.cornerRadius = GAP;
        self.layer.masksToBounds = YES;
        self.backgroundColor = RGBA(230, 230, 230, 0.5);
        self.label.font = [UIFont systemFontOfSize:20 weight:UIFontWeightLight];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(window);
            make.size.mas_offset(self.frame.size);
        }];
    }
    return self;
}

- (HYLabel *)label
{
    if (!_label)
    {
        _label = [[HYLabel alloc]init];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
    }
    return _label;
}

/**
 *@pragma 在window中央，弹出索引字母
 */
- (void)showMessage:(NSString *)message hidden:(BOOL)hidden
{
    self.label.text = message;
    self.hidden = NO;
    dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) 1.3 *NSEC_PER_SEC);//1.3秒
    dispatch_after(delaytime, dispatch_get_main_queue(), ^{
        if (hidden)
        {
            self.hidden = YES;
        }
    });
}

//+ (void)showWithTitle:(NSString *)title message:(NSString *)message inController:(UIViewController *)controller complete:(void(^)(void))completion{
//    
//    WS(controller)
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
//                                                                     message:message
//                                                              preferredStyle:UIAlertControllerStyleAlert];
//    
//    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定"
//                                                       style:UIAlertActionStyleDefault
//                                                     handler:^(UIAlertAction * _Nonnull action) {
//                                                         if (completion)
//                                                             completion();
//                                                     }];
//    [alertVC addAction:enterBtn];
//    [weakSelf presentViewController:alertVC animated:YES completion:nil];
//}
//
//+ (void)showInWindowWithTitle:(NSString *)title message:(NSString *)message complete:(void(^)(void))completion{
//    
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
//                                                                     message:message
//                                                              preferredStyle:UIAlertControllerStyleAlert];
//    
//    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
//                                                     handler:^(UIAlertAction * _Nonnull action) {
//                                                         if (completion)
//                                                             completion();
//                                                     }];
//    [alertVC addAction:enterBtn];
//    
//    UIWindow *window = [UIApplication sharedApplication].delegate.window;
//    [window makeKeyAndVisible];
//    if (window.rootViewController.presentedViewController)
//        [window.rootViewController.presentedViewController presentViewController:alertVC animated:YES completion:nil];
//    else
//        [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
//    
//    
//}
//
//+ (void)showInWindowWithTitle:(NSString *)title message:(NSString *)message enterComplete:(void(^)(void))completion{
//    
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
//                                                                     message:message
//                                                              preferredStyle:UIAlertControllerStyleAlert];
//    
//    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"非常确定" style:UIAlertActionStyleDefault
//                                                     handler:^(UIAlertAction * _Nonnull action) {
//                                                         if (completion)
//                                                             completion();
//                                                     }];
//    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel
//                                                      handler:nil];
//    [alertVC addAction:enterBtn];
//    [alertVC addAction:cancelBtn];
//    [[self findPresentedViewController:nil] presentViewController:alertVC animated:YES completion:nil];
//}
//
//+ (void)showMessage:(NSString *)message hidden:(void(^)(void))hidden{
//    
//    HYAlertController *alertVC = [HYAlertController alertControllerWithTitle:nil
//                                                                               message:message
//                                                                        preferredStyle:UIAlertControllerStyleAlert];
//    //修改message
//    NSMutableAttributedString *alertVCMessage = [[NSMutableAttributedString alloc] initWithString:message];
//    //        [alertVCMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
//    [alertVCMessage addAttribute:NSFontAttributeName value:HYAlertMessageFont range:NSMakeRange(0, message.length)];
//    [alertVC setValue:alertVCMessage forKey:@"attributedMessage"];
//    
//    [[self findPresentedViewController:nil] presentViewController:alertVC animated:YES completion:^{
//        dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) 1.3 *NSEC_PER_SEC);//1.3秒
//        dispatch_after(delaytime, dispatch_get_main_queue(), ^{
//            [alertVC dismissViewControllerAnimated:YES completion:^{//1.3秒后，返回
//                if (hidden)
//                {
//                    hidden();
//                }
//            }];
//        });
//    }];
//}
//
//+ (UIViewController*)findPresentedViewController:(UIViewController *)VC
//{
//    if (VC == nil)
//    {
//        UIViewController *newVC = [UIApplication sharedApplication].delegate.window.rootViewController;
//        return [self findPresentedViewController:newVC];
//    }
//    else if (VC.presentedViewController)
//    {
//        return [self findPresentedViewController:VC.presentedViewController];
//    }
//    else
//        return VC;
//}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
